package com.mux.springbootjsp.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
public class WebSecurityConfig  extends WebSecurityConfigurerAdapter{
	
	 @Override
	    protected void configure(HttpSecurity http) throws Exception {
		 http.csrf().disable();
	        http
	            .authorizeRequests()
	                .antMatchers("/", "/home").permitAll()
	              //  .antMatchers("/", "/home","/static/**","/**/*.js").permitAll()
	             //   .antMatchers("/admin/**").hasRole("ADMIN")
	                .anyRequest().authenticated()
	                .and()
	            .formLogin()
	                .loginPage("/login")
	                .permitAll()
	                .and()
	            .logout()
	                .permitAll();
	       /* 
	        http.authorizeRequests().antMatchers("/admin/**").hasRole("ROLE_ADMIN")
	        .antMatchers("/user/**").hasAnyRole("ROLE_ADMIN","ROLE_USER").anyRequest().authenticated();*/
	    }

	 	/**
	 	 * 内存中的用户
	 	 * @param auth
	 	 * @throws Exception
	 	 */
	  @Override
	   public void configure(AuthenticationManagerBuilder auth) throws Exception {
	    	//内存中的用户
	        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN")
	        .and() .withUser("user").password("abcd").roles("USER");
	    }
	  
	 
	 /**
	  * jdbc 中的用户
	  */
	/*  @Autowired
	  DataSource dataSource;
	  @Override
	   public void configure(AuthenticationManagerBuilder auth) throws Exception {
		  auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery( 
				  "select username,password, enabled from users where username=?") 
		  .authoritiesByUsernameQuery( 
		  "select username, role from user_roles where username=?"); 
	  }*/
}
